package transaction;

public class RIFactory {
	
	public static ResourceItem getResourceItem(String type, Object key, String [] colVals)
	{
		String [] colNames = null;
		if(type.equals(ResourceManager.RMINameCars))
		{
			colNames= new String[]{"locatoin", "price", "numCars", "numAvail"};
		}else if(type.equals(ResourceManager.RMINameCustomers))
		{
			colNames= new String[]{"custName"};
		}else if(type.equals(ResourceManager.RMINameFlights))
		{
			colNames= new String[]{"flightNum", "price", "numSeats", "numAvail"};
		}else if(type.equals(ResourceManager.RMINameRooms))
		{
			colNames= new String[]{"location", "price", "numRooms", "numAvail"};
		}else if(type.equals(ResourceManager.RMINameReservations))
		{
			//no primary key, let resvID be the key = (custName+xid)
			colNames = new String[]{"resvID", "custName", "resvType", "resvKey"};
		}

		if(colNames!=null&&colNames.length==colVals.length)
			return new ResourceItemImpl(key, colNames, colVals);

		return null;
	}

	public static class ResourceItemImpl implements ResourceItem
	{
		private String [] colNames = null;
		private String [] colVals = null;
		private boolean isDeleted = false;
		private Object key = null;
		
		public ResourceItemImpl(Object key, String [] colNames, String [] colVals) {
			this.key = key;
			this.colNames = colNames;
			this.colVals = colVals;
		}

		@Override
		public String[] getColumnNames() {
			// TODO Auto-generated method stub
			return colNames;
		}

		@Override
		public String[] getColumnValues() {
			// TODO Auto-generated method stub
			return colVals;
		}

		@Override
		public Object getIndex(String indexName) throws InvalidIndexException {
			// TODO Auto-generated method stub
			int i = 0;
			for(String idx:colNames)
			{
				if(idx.equals(indexName))
					break;
				i++;
			}
			if(i>=colNames.length)
				return null;
			return colVals[i];
		}

		@Override
		public Object getKey() {
			return key;
		}

		@Override
		public boolean isDeleted() {
			// TODO Auto-generated method stub
			return isDeleted;
		}

		@Override
		public void delete() {
			// TODO Auto-generated method stub
			this.isDeleted = true;
		}
		
		public Object clone(){
			return new ResourceItemImpl(key, colNames.clone(), colVals.clone());
		}
		
	}
}
